package com.gitee.huanminabc.jcommon.container;

import java.lang.reflect.Array;

/**
 * 简要描述
 *
 * @Author: huanmin
 * @Date: 2025/4/9 00:51
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
public class ArrayUtil {

    //contains
    public static <T> boolean contains(T[] arr, T element) {
        if (arr == null) {
            return false;
        }
        for (T item : arr) {
            if (item.equals(element)) {
                return true;
            }
        }
        return false;
    }
    //isEmpty
    public static <T> boolean isEmpty(T[] arr) {
        return arr == null || arr.length == 0;
    }

    public static <T extends Comparable<T>> void  reverse(T[] arr) {
        int left = 0;
        int right = arr.length - 1;

        while (left < right) {
            T temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;

            left++;
            right--;
        }
    }

    //将A数组的元素添加到B数组中
    public static <T> T[] addAll(T[] a, T[] b) {
        int aLength = a.length;
        int bLength = b.length;
        T[] result = (T[]) Array.newInstance(a.getClass().getComponentType(), aLength + bLength);
        System.arraycopy(a, 0, result, 0, aLength);
        System.arraycopy(b, 0, result, aLength, bLength);
        return result;
    }
    //拷贝数组
    public static <T> T[] copyOf(T[] original, int newLength) {
        T[] copy = (T[]) Array.newInstance(original.getClass().getComponentType(), newLength);
        System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
        return copy;
    }

    //克隆
    public static <T> T[] clone(T[] original) {
        return copyOf(original, original.length);
    }

    //在数组结尾添加元素,如果数组长度不够则扩容
    public static <T> T[] add(T[] original, T element) {
        int length = original.length;
        T[] newArray = (T[]) Array.newInstance(original.getClass().getComponentType(), length + 1);
        System.arraycopy(original, 0, newArray, 0, length);
        newArray[length] = element;
        return newArray;
    }
    //在数组开头添加元素,如果数组长度不够则扩容
    public static <T> T[] addFirst(T[] original, T element) {
        int length = original.length;
        T[] newArray = (T[]) Array.newInstance(original.getClass().getComponentType(), length + 1);
        System.arraycopy(original, 0, newArray, 1, length);
        newArray[0] = element;
        return newArray;
    }
    //在数组中间添加元素,如果数组长度不够则扩容
    public static <T> T[] addMiddle(T[] original, T element, int index) {
        int length = original.length;
        if (index < 0 || index > length) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
        }
        T[] newArray = (T[]) Array.newInstance(original.getClass().getComponentType(), length + 1);
        System.arraycopy(original, 0, newArray, 0, index);
        newArray[index] = element;
        System.arraycopy(original, index, newArray, index + 1, length - index);
        return newArray;
    }

    //截取数组
    public static <T> T[] subArray(T[] original, int start, int end) {
        if (start < 0 || end > original.length || start > end) {
            throw new IndexOutOfBoundsException("Start: " + start + ", End: " + end + ", Length: " + original.length);
        }
        T[] newArray = (T[]) Array.newInstance(original.getClass().getComponentType(), end - start);
        System.arraycopy(original, start, newArray, 0, end - start);
        return newArray;
    }









    // 基本数据类型数组反转
    public static void reverse(Object array) {
        int length = Array.getLength(array);
        Object temp = null;// 临时变量
        for (int i = 0; i < length / 2; i++) {
            temp =Array.get(array,i) ;
            Array.set(array,i,Array.get(array,length - 1 - i));
            Array.set(array,length - 1 - i,temp);
        }
    }

    //判断List是否是有序的, 采用的策略是尽可能的少比较
    public static <T extends Comparable<T>> boolean isOrder(T[] data) {
        //判断开头中间结尾是否是无序的
        if (data[0].compareTo(data[1]) > 0 || data[data.length / 2 - 1].compareTo(data[data.length / 2]) > 0 || data[data.length - 2].compareTo(data[data.length - 1]) > 0) {
            return false;
        }

        //100数据以内挨个比较
        if (data.length <= 100) {
            return isOrder(data, 0, data.length- 1);
        } else if (data.length <= 1000) {//1000数据取开头中间结尾各50数据比较
            return isOrder(data, 0, 50) && isOrder(data, data.length / 2 - 25, data.length / 2 + 25) && isOrder(data, data.length - 50, data.length- 1);
        } else if (data.length<= 10000) {//10000数据取,随机取10个50个范围数据比较
            for (int i = 0; i < 10; i++) {
                int start = (int) (Math.random() * (data.length - 50));
                if (!isOrder(data, start, start + 50)) {
                    return false;
                }
            }
            return true;
        }else {//10000以上数据取,每隔1000个数据取50个范围数据比较
            for (int i = 0; i < data.length; i+=1000) {
                if (!isOrder(data, i, i + 50)) {
                    return false;
                }
            }
            return true;
        }

    }

    //随机判断几个范围内的数是否是有序的
    private static <T extends Comparable<T>> boolean isOrder(T[] data, int start, int end) {
        int length = data.length;
        if (start < 0 || end > length - 1) {
            throw new RuntimeException("start or end is error");
        }
        for (int i = start; i < end; i++) {
            if (data[i].compareTo(data[i + 1]) > 0) {
                return false;
            }
        }
        return true;
    }


}
